package com.cgbl.cg.vm.collection

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse
import org.w3c.dom.Text

class DiscoveryCollectionViewModel(application: Application) : GLBaseViewModel(application) {
    var id = ""
    val typeName = ObservableField<String>()
    val list = arrayListOf<CourseAllTypeBean>()
    val uc = UiChangeEvent()

    var pageNo = 1

    class UiChangeEvent {
        val selectType: SingleLiveEvent<ArrayList<String>> = SingleLiveEvent()
        val refreshEvent = SingleLiveEvent<Int>()
        val hotCollectData = SingleLiveEvent<List<CollectDetailBean>>()
        val refreshDataEvent = SingleLiveEvent<List<WorksBean>>()
    }

    val showDialogCommand = BindingCommand<Any>(BindingAction {
        uc.selectType.call()
    })

    val toSelectCollectionCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.COLLECTION.A_SELECT_COLLECTION)
    })

    val toReleaseWorkCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.WORK.A_RELEASE_WORK)
    })

    fun refresh() {
        pageNo = 1
        page()
        hotCollect()
    }

    fun next() {
        pageNo++
        page()
    }

    fun page() {
        if (TextUtils.isEmpty(id))
            return
        WorkServiceFactory.page(
            mapOf(
                "pageNo" to pageNo,
                "pageSize" to "20",
                "searchType" to 0,
                "workTypeId" to id,
                "isAccount" to "0",
                "type" to "1"
            )
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<WorksBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<WorksBean>>) {
                uc.refreshDataEvent.postValue(t.data.list)
                uc.refreshEvent.postValue(
                    if (t.data.total.toInt() < pageNo * 20) {
                        0
                    } else {
                        1
                    }
                )
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(0)
            }
        })
    }

    fun allType() {
        WorkServiceFactory.allType()
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<List<CourseAllTypeBean>>>(this) {
                override fun onNext(t: BaseResponse<List<CourseAllTypeBean>>) {
                    list.addAll(t.data)
                    if (!TextUtils.isEmpty(id)) {
                        val bean = t.data.find { it.id == id }
                        typeName.set(bean?.name)
                    } else {
                        if (!t.data.isNullOrEmpty()) {
                            id = t.data[0].id ?: ""
                            typeName.set(t.data[0].name)
                            refresh()
                        }
                    }
                }
            })
    }

    fun hotCollect(){
        WorkServiceFactory.hotCollect(mapOf())
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<List<CollectDetailBean>>>(this){
                override fun onNext(t: BaseResponse<List<CollectDetailBean>>) {
                    uc.hotCollectData.postValue(t.data)
                }
            })
    }
}
